package com.jingyuan.client;

import com.jingyuan.common.entity.HostInfo;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 地址管理区
 */
@Setter
@Getter
public class HostAddressManager {
    private String masterAddress;   //Master主机地址
    private int masterPort;      //master主机
    private boolean isOnVoting; //是否正在选举中
    private boolean isMaster;
    private List<HostInfo> addressList=new CopyOnWriteArrayList<>();
    private static HostAddressManager manager=new HostAddressManager();

    private HostAddressManager(){

    }

    public static HostAddressManager getHostAddressManager(){
        return manager;
    }

    /**
     * 添加主机
     * @param hostInfo
     */
    public void addHost(HostInfo hostInfo){
        addressList.add(hostInfo);
    }

    /**
     * 批量添加主机
     * @param hostInfos
     */
    public void addHost(List<HostInfo> hostInfos){
        addressList.addAll(hostInfos);
    }

    /**
     * 移除主机
     * @param address
     */
    public void removeHost(String address){
        addressList.removeIf(hostInfo -> {
            return hostInfo.getAddress().equals(address);
        });
    }
}
